package utils

import (
	"crypto/md5"
	"crypto/sha512"
	"encoding/hex"
	"fmt"
	"github.com/anaskhan96/go-password-encoder"
	"io"
	"strings"
)

/**
 * md5加密
 * @author feige
 * @date 2024-04-06
 * @version 1.0
 * @desc
 */
func MD5(value string) string {
	hash := md5.New()
	_, _ = io.WriteString(hash, value)
	return hex.EncodeToString(hash.Sum(nil))
}

func PasswordEncoder(pwd string) string {
	options := &password.Options{16, 100, 32, sha512.New}
	salt, encodedPwd := password.Encode(pwd, options)
	return fmt.Sprintf("$pbkdf2-sha512$%s$%s", salt, encodedPwd)
}

func PasswordVerfiy(pwd string, encodePwd string) bool {
	options := &password.Options{16, 100, 32, sha512.New}
	passwordInfo := strings.Split(encodePwd, "$")
	return password.Verify(pwd, passwordInfo[2], passwordInfo[3], options)
}

//func main()  {
//	// 启动gin
//	pwd := "123456"
//	passwordEncoder := utils.PasswordEncoder(pwd)
//	fmt.Println(passwordEncoder)
//	verfiy := utils.PasswordVerfiy(pwd, "$pbkdf2-sha512$cFAoFHyTWtenIpK1$c497c66d35bc3ff150de5ef3da745c482b5dfcd0bdd78d1e8efe29791ef6e092")
//	fmt.Println(verfiy)
//}
